package messageHandler;

import java.io.IOException;
import java.util.Vector;

import main.GameCanvas;
import main.NuoiThuMidlet;
import model.Animal;
import model.AnimalInfo;
import model.Avatar;
import model.AvatarInfo;
import model.Command;
import model.DataManager;
import model.DrugInfo;
import model.FoodInfo;
import model.IAction;
import model.ImageData;
import model.Item;
import model.ItemInfo;
import model.MoneyInfo;
import model.PlayerInfo;
import model.Position;
import model.Quest;
import model.Toy;
import model.ToyInfo;
import network.Cmd_Message;
import network.IMessageHandler;
import network.Message;
import screen.AnimalListSrc;
import screen.ChatTab;
import screen.HydridsAnimalScr;
import screen.MainScr;
import screen.MsgScreen;
import screen.RoomListScr;
import screen.Screen;
import service.GlobalService;
import utilities.CRes;
import utilities.GameResource;
import utilities.TileMap;
import animation.FlyText;

public class GlobalMsgHandler implements IMessageHandler {
    static GlobalMsgHandler m_instance = null;
    public IMiniGameMsgHandler miniGameMessageHandler;
    
    int pricetask;

    public static GlobalMsgHandler gI() {
        if (m_instance == null) {
            m_instance = new GlobalMsgHandler();
        }
        return m_instance;
    }

    public void onMessage(Message msg) {
        // TODO Auto-generated method stub
    	
        try {
            switch (msg.command) {
            case Cmd_Message.LOGIN_SUCCESS:
                try {
                    NuoiThuMidlet.m_myPlayerInfo = null;
                    GameCanvas.mainScr.m_friend = null;
                    
                    NuoiThuMidlet.m_myPlayerInfo = new PlayerInfo();
                    NuoiThuMidlet.m_myPlayerInfo.m_iId = msg.reader().readInt();
                    NuoiThuMidlet.m_myPlayerInfo.m_UserName = msg.reader().readUTF();
                    NuoiThuMidlet.m_myPlayerInfo.setShortName(NuoiThuMidlet.m_myPlayerInfo.m_UserName);
                    NuoiThuMidlet.m_myPlayerInfo.m_level = msg.reader().readInt();

                    //msg.reader().readShort();
                    NuoiThuMidlet.m_myPlayerInfo.m_iMoney[0] = msg.reader().readInt();
                    NuoiThuMidlet.m_myPlayerInfo.m_iMoney[1] = msg.reader().readInt();
                    //NuoiThuMidlet.m_myPlayerInfo.setAvatar(msg.reader().readInt());
                    NuoiThuMidlet.m_myPlayerInfo.m_avatarId = msg.reader().readInt();
                    
                    NuoiThuMidlet.m_myPlayerInfo.m_sExperience = msg.reader().readInt();
                    NuoiThuMidlet.m_myPlayerInfo.fullName = msg.reader().readUTF();
                    NuoiThuMidlet.m_myPlayerInfo.status = msg.reader().readUTF();
                    NuoiThuMidlet.m_myPlayerInfo.mobile = msg.reader().readUTF();
                    NuoiThuMidlet.m_myPlayerInfo.gender = msg.reader().readByte();
                    
                    //reset old message
                    Screen.m_nMsg = 0;
                } catch (Throwable e) {
                }
                //NuoiThuMidlet.m_myPlayerInfo.addfriendStatus = CRes.loadRMSInt("addFriendStatus");
                GlobalService.gI().getListImage();
               // GlobalService.gI().getListItemType();
                break;
                
            case Cmd_Message.REGISTER:
            	byte card = msg.reader().readByte();
            	String sms = msg.reader().readUTF();
            	String ds = msg.reader().readUTF();
            	if(card == 0)
            	{
            		GameCanvas.startOKDlg(sms);
            	}
            	else
            	{
            		GameCanvas.loginScr.onSendSMS(sms, ds);
            	}
            	break;
            case Cmd_Message.FORUM_LINK:
            	String link = msg.reader().readUTF();
            	MainScr.gI().onReceivedForumLink(link);
            	break;
            	
            case Cmd_Message.GET_PASSWORD:
            	GameCanvas.endDlg();
            	
            	sms = msg.reader().readUTF();
            	ds = msg.reader().readUTF();
            	
            	GameCanvas.loginScr.onSendSMS(sms, ds);            	
            	break;
                
            case Cmd_Message.CHANGE_PASSW:
                NuoiThuMidlet.sendSMS(msg.reader().readUTF(), msg.reader().readUTF(), new IAction() {
					
					public void perform() {
						// TODO Auto-generated method stub
						GameCanvas.startOKDlg(GameResource.smsSent);
					}
				}, new IAction() {
					
					public void perform() {
						// TODO Auto-generated method stub
						GameCanvas.startOKDlg(GameResource.sendSMSFail);
					}
				});
                break;
                
            case Cmd_Message.MONEY_INCOME:
                byte moneyType = msg.reader().readByte();
                int value = msg.reader().readInt();
                String str = msg.reader().readUTF();

                MsgScreen.gI().onChatFrom(-9999, GameResource.admin, str);
                Screen.m_nMsg++;
                
                NuoiThuMidlet.m_myPlayerInfo.addMoney(value, moneyType);
                
                FlyText fly = new FlyText();
                
                str = "+";
                if(value < 0)
                	str = "";
                fly.startEffect(str + value, GameCanvas.hw, GameCanvas.hh, 0xffff00, 0x000000);

                
                break;
                
            case Cmd_Message.GET_LIST_IMAGE:
            	short count = msg.reader().readByte();
            	int i;
            	Vector vtImg = new Vector();
            	//ImageData[] arrImg = new ImageData[count];
                for(i = 0; i < count; i++){
                	ImageData img = new ImageData();
                
                	img.m_sImgId = msg.reader().readShort();
                	img.m_sImgVersion = msg.reader().readShort();
                	
                	vtImg.addElement(img);
            	}
            	DataManager.gI().checkImageData(vtImg);            	
            	break;
            	
            case Cmd_Message.DIARY:
                count = msg.reader().readShort();
                Vector vDiary = new Vector();
                for (i = 0; i < count; i++) {
                    Item item = new Item();
                    item.m_strName = msg.reader().readUTF();
                    vDiary.addElement(item);
                }
                MainScr.gI().onListDiary(vDiary);
                break;
                
            case Cmd_Message.TOP_PLAYER:
                int type = msg.reader().readShort();
                count = msg.reader().readShort();
                Vector listTopPlayers = new Vector();
                for (int j = 0; j < count; j++) {
                    PlayerInfo p = new PlayerInfo();
                    p.m_iId = msg.reader().readInt();
                    p.m_UserName = msg.reader().readUTF();
                    p.setShortName(p.m_UserName);
                    p.m_level = msg.reader().readInt();
                    p.m_iMoney[0] = msg.reader().readInt();
                    p.m_iMoney[1] = msg.reader().readInt();
                    p.setAvatar(msg.reader().readInt());
                    p.m_onlineStatus = msg.reader().readBoolean();
                    p.status = msg.reader().readUTF();
                    p.m_sExperience = msg.reader().readInt();
                    p.m_bIsDrawRichest = true;
                    
                    listTopPlayers.addElement(p);
                }
                MainScr.gI().selectedRankIndex = type;
                MainScr.gI().onListTopPlayers(listTopPlayers);
                break;
            
            	
            case Cmd_Message.GET_IMAGE:
            	ImageData imgData = new ImageData();
            	imgData.m_sImgId = msg.reader().readShort();
            	imgData.m_sImgVersion = msg.reader().readShort();
            	int length = msg.reader().readShort();
            	imgData.data = new byte[length];
            	for(i = 0; i < length; i++)
            		imgData.data[i] = msg.reader().readByte();

            	DataManager.gI().saveImgData(imgData);
            	break;
              
            case Cmd_Message.LIST_ITEM_TYPE:
                int n = msg.reader().readByte();
                Vector listItemType = new Vector();
                for (i = 0; i < n; i++) {
                    ItemInfo itemInfo = new ItemInfo();
                    itemInfo.m_bItemType = msg.reader().readByte();
                    itemInfo.m_strName = msg.reader().readUTF();
                    itemInfo.m_iVersion = msg.reader().readInt();
                    listItemType.addElement(itemInfo);
                }
                DataManager.gI().checkGardenData(listItemType);
                break;
                
            case Cmd_Message.MARQUEE_MESSAGE:
            	str = msg.reader().readUTF();
				GameCanvas.currentScreen.onMarqueeMsg(str);
				break;
				
            case Cmd_Message.GET_LIST_ITEM:
                byte itemType = msg.reader().readByte();
                switch (itemType) {
                    case Item.ITEM_TYPE_ANIMAL:
                        readListAnimal(msg);
                        break;
                    case Item.ITEM_TYPE_TOY:
                        readListToy(msg);
                        break;
                    case Item.ITEM_TYPE_AVATAR:
                        readListAvatar(msg);
                        break;
                    case Item.ITEM_TYPE_FOOD:
                    	readListFood(msg);
                    	break;
                    case Item.ITEM_TYPE_DRUG:
                    	readListDrug(msg);
                    	break;
                }
                break;

            case Cmd_Message.GET_GARDEN_DATA:
                //read user id
                int userID = msg.reader().readInt();
                Vector vtAva = null;
                Vector vectorMyItem = null;
                short itemID;
                if(userID == NuoiThuMidlet.m_myPlayerInfo.m_iId){
                	if(NuoiThuMidlet.m_myPlayerInfo.m_defaultAvatar == null){
                		NuoiThuMidlet.m_myPlayerInfo.setAvatar(NuoiThuMidlet.m_myPlayerInfo.m_avatarId);
                	}
                	
                	 vtAva = new Vector();
                     count = msg.reader().readShort();
                     for(i = 0; i < count; i++){
                     	itemID = msg.reader().readShort();
                     	Avatar ava = new Avatar(itemID);
                     	ava.m_iId = msg.reader().readShort();
                     	ava.m_iTimeUsing = msg.reader().readShort();
                     	ava.m_bIsUsing = msg.reader().readBoolean();
                     	ava.m_iNumItem = 1;
                     	ava.m_strName = ava.itemInfo.m_strName + " " + ava.m_iId;
                     	
                     	if(!ava.m_bIsUsing)
                     		vtAva.addElement(ava);
                     }
                     
                     //read items + products
                     count = msg.reader().readShort();
                     vectorMyItem = new Vector();
                     for (i = 0; i < count; i++) {
                         Item item = new Item();
                         
                         item.m_bItemType = msg.reader().readByte();
                         item.m_iId = msg.reader().readShort();
                         item.m_iNumItem = msg.reader().readShort();
                         if (item.m_iId <= 0) {
                             item = null;
                             continue;
                         }

                         vectorMyItem.addElement(item);
                     }

                     DataManager.gI().setMyListItem(vectorMyItem);
                     CRes.cleanVector(DataManager.gI().m_myLstAvatar);
                     DataManager.gI().m_myLstAvatar = vtAva;
                }
                
                //read animals
                count = msg.reader().readShort();
                Vector vectorAnimal = new Vector();
                for (i = 0; i < count; i++) {
                	int animalType = msg.reader().readShort();
                    Animal a = new Animal(animalType);
                	
                    a.m_iId = msg.reader().readShort();
                    a.setTimeLife(msg.reader().readShort());
                    a.m_iLost = msg.reader().readShort();
                    a.m_iHealth = msg.reader().readShort();
                    a.m_iSick = msg.reader().readShort();
                    a.m_iSmart = msg.reader().readShort();
                    a.m_iGender = msg.reader().readByte();
                    a.m_bIsDie = msg.reader().readBoolean();
                    a.m_bIsDirty = msg.reader().readBoolean();
                    a.m_bIsHungry = msg.reader().readBoolean();
                    a.m_bIsHaveMoney = msg.reader().readBoolean();
                    a.m_iBonusHealth = msg.reader().readShort();
                    a.m_iBonusTime = msg.reader().readShort();
                                        
                    a.m_strName = msg.reader().readUTF();
                    if(a.m_strName.length() == 0){
                    	a.m_strName = a.animalInfo.m_strName + " " + a.m_iId;
                    }
                    
                    TileMap.instance.loadMap(TileMap.GARDEN_MAP);
                    int tmpY = TileMap.m_iHeightTile * TileMap.instance.m_gardenMap.m_iMapHeight;
                    int tmpYY = GameCanvas.h - tmpY*2/3 + 10;
        			
                    a.m_iLeft = 50;
                    a.m_iTop = tmpYY +35;
                    a.m_iBoundH = tmpY*2/3 - GameCanvas.hBottomBar - 45;
                    a.m_iBoundW = GameCanvas.w -70;
                    
                    a.start(new Position(CRes.random(a.m_iLeft, a.m_iLeft+a.m_iBoundW), CRes.random(a.m_iTop, a.m_iTop+a.m_iBoundH)));
                                        
                    vectorAnimal.addElement(a);
                }

                //read toys					
                count = msg.reader().readShort();
                Vector vtToyInUse = new Vector();
                Vector vtToyInStock = new Vector();
                for (i = 0; i < count; i++) {
                	itemID = msg.reader().readShort();
                	Toy toy = new Toy(itemID);
                    toy.m_iId = msg.reader().readShort();
                    toy.m_iTileX = msg.reader().readShort();
                    toy.m_iTileY = msg.reader().readShort();
                    toy.m_X = toy.m_iTileX * TileMap.m_iWidthTile;
                    toy.m_Y = GameCanvas.h - GameCanvas.hBottomBar - toy.m_iTileY * TileMap.m_iHeightTile;
                    
                    toy.m_iTimeUsing = msg.reader().readShort();
                    toy.m_iNumItem = 1;
                    toy.m_strName = toy.itemInfo.m_strName + " " + toy.m_iId;
                    
                    if(toy.m_iTileX == -1 && toy.m_iTileY == -1){
                    	vtToyInStock.addElement(toy);
                    }
                    else
                    {
                    	vtToyInUse.addElement(toy);
                    }                                        
                }
               
                if(NuoiThuMidlet.m_myPlayerInfo.m_iId == userID){
                	DataManager.gI().m_myLstToy = vtToyInStock;
                	
                }
                else{
                	CRes.cleanVector(vtToyInStock);
                }
                
                GameCanvas.mainScr.setGardenData(userID, vectorAnimal, vtToyInUse);
                GameCanvas.mainScr.switchToMe();
                GameCanvas.mainScr.m_bIsOpenScrAnimation = true;                
                break;
                
            case Cmd_Message.SERVER_MESSAGE:
                GameCanvas.startOKDlg(msg.reader().readUTF());
                break;
               
            case Cmd_Message.SERVER_CHAT:
                str = msg.reader().readUTF();
                MsgScreen.gI().onChatFrom(-9999, GameResource.admin, str);
                MainScr.m_nMsg++;
                break;
                
            case Cmd_Message.BUY_ITEM:
                itemType = msg.reader().readByte();
                if(itemType == Item.ITEM_TYPE_ANIMAL){
                	itemID = msg.reader().readShort();
                    short animalID = msg.reader().readShort();
                    byte gender = msg.reader().readByte();
                    byte typeMoney = msg.reader().readByte();
                    int moneyBuy = msg.reader().readInt();
                    int exp = msg.reader().readShort();
                    MainScr.gI().onBuyAnimal(itemType, itemID, animalID, gender, typeMoney, moneyBuy, exp);
                }
                else if(itemType == Item.ITEM_TYPE_TOY){
                    itemID = msg.reader().readShort();
                    short toyId = msg.reader().readShort();
                    byte typeMoney = msg.reader().readByte();
                    int moneyBuy = msg.reader().readInt();
                    int exp = msg.reader().readShort();
                    MainScr.gI().onBuyToy(itemType, itemID, toyId, typeMoney, moneyBuy, exp);
                }else {
                	itemID = msg.reader().readShort();
                    short numItem = msg.reader().readShort();
                    byte typeMoney = msg.reader().readByte();
                    int moneyBuy = msg.reader().readInt();
                    int exp = msg.reader().readShort();
                    MainScr.gI().onBuyItem(itemType, itemID, numItem, typeMoney, moneyBuy, exp);
                }
                break;
                
            case Cmd_Message.SELL_PET:
            	byte option = msg.reader().readByte();
            	int gardenID = msg.reader().readInt();
            	short animalType = msg.reader().readShort();
            	short animalId = msg.reader().readShort();
            	short price = msg.reader().readShort();
            	MainScr.gI().onSellPet(option, gardenID, animalType, animalId, price);	
            	break;
            	
            case Cmd_Message.GO_DOCTOR:
            	gardenID = msg.reader().readInt();
            	animalType = msg.reader().readShort();
            	animalId = msg.reader().readShort();
            	short sick = msg.reader().readShort();
            	short drugID = msg.reader().readShort();
            	MainScr.gI().onGoDoctor(gardenID, animalType, animalId, sick, drugID);
            	break;
            	
            case Cmd_Message.HYDRIDS_ANIMAL:
            	boolean result = msg.reader().readBoolean();
            	animalType = msg.reader().readShort();
            	animalId = msg.reader().readShort();
            	byte gender = msg.reader().readByte();
            	byte bonusHealth = msg.reader().readByte();
            	
            	HydridsAnimalScr.gI().setListItem(AnimalListSrc.gI().getListItemChoose());
            	HydridsAnimalScr.gI().onCompleteHydrid(result, animalType, animalId, bonusHealth, gender);	
            	            	            	
            	AnimalListSrc.gI().close();
				GameCanvas.endDlg();
				HydridsAnimalScr.gI().switchToMe();
            	
            	break;
            	
            case Cmd_Message.SET_PET_PLAYGAME:
            	animalType = msg.reader().readShort();
            	animalId = msg.reader().readShort();
            	gardenID = msg.reader().readInt();
            	short gameId = msg.reader().readShort();
            	//request play game

				GlobalService.gI().setHandler(gameId);
            	break;
            	
            case Cmd_Message.GET_FRIENDLIST:
                count = msg.reader().readShort();
                
                Vector vtFriends = new Vector();
                for (i = 0; i < count; i++) {
                    PlayerInfo p = new PlayerInfo();
                    p.m_iId = msg.reader().readInt();
                    p.m_UserName = msg.reader().readUTF();
                    p.setShortName(p.m_UserName);
                    p.m_level = msg.reader().readInt();
                    p.m_iMoney[0] = msg.reader().readInt();
                    p.m_iMoney[1] = msg.reader().readInt();
                    p.m_onlineStatus = msg.reader().readBoolean();
                    p.setAvatar(msg.reader().readInt());
                    p.m_sExperience = msg.reader().readInt();
                    p.status = msg.reader().readUTF();
                    p.gender = msg.reader().readByte();
                    p.mobile = msg.reader().readUTF();
                    
                    vtFriends.addElement(p);               
                }
                MainScr.gI().onFriendList(vtFriends);
                break;
                
            case Cmd_Message.ADD_FRIEND:
                PlayerInfo p = new PlayerInfo();

                p.m_iId = msg.reader().readInt();
                p.m_UserName = msg.reader().readUTF();
                p.setShortName(p.m_UserName);
                p.m_level = msg.reader().readInt();
                p.m_iMoney[0] = msg.reader().readInt();
                p.m_iMoney[1] = msg.reader().readInt();
                p.setAvatar(msg.reader().readShort());
                boolean accept = msg.reader().readBoolean();
                if (accept) {
                    GameCanvas.startOKDlg(p.m_UserName + GameResource.acceptMakeFriend);
                    /*
                    p.m_onlineStatus = true;
                    if (!DataManager.gI().m_myListFriend.contains(p)) {
                    	DataManager.gI().m_myListFriend.addElement(p);
                    }
                    */
                    DataManager.gI().m_myListFriend.removeAllElements();
                    DataManager.gI().m_myListFriend = null;
                } else {
                    GameCanvas.startOKDlg(p.m_UserName + GameResource.disagree);
                    p = null;
                }
                break;
                
            case Cmd_Message.REQUEST_ADD_FRIEND:
                p = new PlayerInfo();

                p.m_iId = msg.reader().readInt();
                p.m_UserName = msg.reader().readUTF();
                p.setShortName(p.m_UserName);
                p.m_level = msg.reader().readInt();
                p.m_iMoney[0] = msg.reader().readInt();
                p.m_iMoney[1] = msg.reader().readInt();
                p.m_onlineStatus = true;

                if(MsgScreen.gI().findTab(p.m_iId) != null)
            		return;
        		    	    	
                final int playerId = p.m_iId;
            	ChatTab ct = new ChatTab(GameResource.addFreind, p.m_iId, new Command(GameResource.yes, new IAction() {
        			public void perform() {
        				ChatTab ct = MsgScreen.gI().getTab(MsgScreen.gI().selectedTab);
        				
        				GlobalService.gI().doAddFriend(playerId, true);
                        
                        MsgScreen.gI().removeTab(ct);
        			}
        		}), new Command(GameResource.no, new IAction() {
        			public void perform() {
        				ChatTab ct = MsgScreen.gI().getTab(MsgScreen.gI().selectedTab);

        				GlobalService.gI().doAddFriend(playerId, false);
                        
        				MsgScreen.gI().removeTab(ct);
        			}
        		}), false);
            	
            	MsgScreen.gI().addRequest(ct);
            	
        		ct.addText(p.m_UserName + GameResource.wantToMakeFriend);
        		
        		if (GameCanvas.currentScreen != MsgScreen.gI())
        			Screen.m_nMsg++;
                break;

            case Cmd_Message.CHAT_WITH:
                int playerID = msg.reader().readInt();
                String playerName = msg.reader().readUTF();
                String content = msg.reader().readUTF();
                MainScr.gI().onChatFrom(playerID, playerName, content);
                break;
                
            case Cmd_Message.SEARCH_PLAYER:
                count = msg.reader().readShort();
                Vector listPlayer = new Vector();
                for (i = 0; i < count; i++) {
                    PlayerInfo player = new PlayerInfo();

                    player.m_iId = msg.reader().readInt();
                    player.m_UserName = msg.reader().readUTF();
                    player.setShortName(player.m_UserName);
                    player.m_level = msg.reader().readInt();
                    player.m_iMoney[0] = msg.reader().readInt();
                    player.m_iMoney[1] = msg.reader().readInt();
                    player.m_onlineStatus = msg.reader().readBoolean();
                    player.setAvatar(msg.reader().readInt());
                    
                    if (player.m_onlineStatus) {
                        listPlayer.addElement(player);
                    } else {
                        player = null;
                    }
                }

                MainScr.gI().onSearchPlayer(listPlayer);
                break;
            case Cmd_Message.FEED_ANIMAL:
            	gardenID = msg.reader().readInt();
				animalType = msg.reader().readShort();
				animalId = msg.reader().readShort();
				short health = msg.reader().readShort();
				short foodID = msg.reader().readShort();
				short num = msg.reader().readShort();
				MainScr.gI().onFeedPet(gardenID, animalType, animalId, health, foodID, num);
            	break;
            
            case Cmd_Message.DO_WATER:
            	gardenID = msg.reader().readInt();
            	animalType = msg.reader().readShort();
            	animalId = msg.reader().readShort();
            	MainScr.gI().onTakeABathPet(gardenID, animalType, animalId);
            	break;
            case Cmd_Message.HARVEST_MONEY:
            	gardenID = msg.reader().readInt();
				animalType = msg.reader().readShort();
				animalId = msg.reader().readShort();
				int money = msg.reader().readShort();
				MainScr.gI().onHarvestMoney(gardenID, animalType, animalId, money);
            	
            	break;
            case Cmd_Message.CHANGE_AVATAR:
            	short avatarId = msg.reader().readShort();
				MainScr.gI().onChangeAvatar(avatarId);
            	break;
            case Cmd_Message.SMS_NAPTIEN:
                Vector vtSms = new Vector();
                while (msg.reader().available() > 0) {
                    MoneyInfo moneyInfo = new MoneyInfo();
                    moneyInfo.info = msg.reader().readUTF();
                    moneyInfo.smsContent = msg.reader().readUTF();
                    moneyInfo.smsTo = msg.reader().readUTF();

                    vtSms.addElement(moneyInfo);
                }
                MainScr.gI().onListSms(vtSms);
              
                break;
                
            case Cmd_Message.SET_TOYS:
            	gardenID = msg.reader().readInt();
            	short toyInfoId = msg.reader().readShort();
            	short toyId = msg.reader().readShort();
            	short tileX = msg.reader().readShort();
            	short tileY = msg.reader().readShort();
            	MainScr.gI().onSetToy(gardenID, toyInfoId, toyId, tileX, tileY);
            	break;
            	
            case Cmd_Message.UPDATE_PROFILE:
            	NuoiThuMidlet.m_myPlayerInfo.fullName = msg.reader().readUTF();
            	NuoiThuMidlet.m_myPlayerInfo.status = msg.reader().readUTF();
            	NuoiThuMidlet.m_myPlayerInfo.mobile = msg.reader().readUTF();
            	NuoiThuMidlet.m_myPlayerInfo.gender = msg.reader().readByte();
            	break;
            	
            case Cmd_Message.REQUEST_PRICE_QUAYSO:
            	int gold = msg.reader().readInt();
            	int dina = msg.reader().readInt();
            	
            	MainScr.gI().onReceivedPriceQuaySo(gold, dina);
            	break;
            	
            case Cmd_Message.QUAYSO:
            	itemType = msg.reader().readByte();
            	itemID = msg.reader().readShort();
            	num = msg.reader().readShort();
            	
            	short option1 = 0, option2 = 0;
            	if(itemType == Item.ITEM_TYPE_ANIMAL){
            		option1 = msg.reader().readShort();//read id animal
            		option2 = msg.reader().readByte();//read gender
            	}
            	else if(itemType == Item.ITEM_TYPE_TOY)
            		option1 = msg.reader().readShort();
            	
            	MainScr.gI().onReceivedGift(itemType, itemID, num, option1, option2);
            	break;
            	
            case Cmd_Message.REQUEST_CURRENT_QUEST:
                short questNumber = msg.reader().readShort();	//nhiem vu thu may
                short bonusMoney1 = msg.reader().readShort();
                short bonusMoney2 = msg.reader().readShort();
                short bonusXp = msg.reader().readShort();
                short itemCount = msg.reader().readShort();
                Vector vtItems = new Vector();
                for (i = 0; i < itemCount; i++) {
                    byte typeItem = msg.reader().readByte();
                    short idItem = msg.reader().readShort();
                    Item item = null;
                    switch (typeItem) {
                        case Item.ITEM_TYPE_ANIMAL:
                            item = new Animal(idItem);
                            break;
                        case Item.ITEM_TYPE_AVATAR:
                            item = new Avatar(idItem);
                            break;
                        case Item.ITEM_TYPE_DRUG:
                            item = new Item();
                            item.itemInfo = DataManager.gI().getDrugInfo(idItem);
                            
                            break;
                        case Item.ITEM_TYPE_FOOD:
                            item = new Item();
                            item.itemInfo = DataManager.gI().getFoodInfo(idItem);
                            break;
                        case Item.ITEM_TYPE_TOY:
                            item = new Toy(idItem);
                            break;
                        default:
                            item = new Item();
                            break;
                    }
                    item.m_bItemType = typeItem;
                    item.m_iId = idItem;
                    item.m_iNumItem = msg.reader().readShort();
                                        
                    vtItems.addElement(item);
                }

                count = msg.reader().readByte();			//so luong nhiem vu
                Vector listTask = new Vector();
                for (i = 0; i < count; i++) {
                    Quest task = new Quest();

                    task.m_bItemType = msg.reader().readByte();
                    task.m_strName = msg.reader().readUTF();
                    task.m_iCurrentNumber = msg.reader().readShort();
                    task.setQuestData(msg.reader().readUTF());

                    listTask.addElement(task);
                }

                MainScr.gI().onRequestListTask(questNumber, listTask, bonusMoney1, bonusMoney2, bonusXp, vtItems);
                break;
            	
            case Cmd_Message.QUEST_COMPLETED:
                questNumber = msg.reader().readShort();
                bonusMoney1 = msg.reader().readShort();
                bonusMoney2 = msg.reader().readShort();
                bonusXp = msg.reader().readShort();
                int level = msg.reader().readInt();
                count = msg.reader().readShort();
                vtItems = new Vector();
                for (i = 0; i < count; i++) {
                    byte typeItem = msg.reader().readByte();
                    short idItem = msg.reader().readShort();
                    Item item = null;
                    switch (typeItem) {
                        case Item.ITEM_TYPE_ANIMAL:
                            item = new Animal(idItem);
                            break;
                        case Item.ITEM_TYPE_AVATAR:
                            item = new Avatar(idItem);
                            break;
                        case Item.ITEM_TYPE_DRUG:
                            item = new Item();
                            item.itemInfo = DataManager.gI().getDrugInfo(idItem);
                            break;
                        case Item.ITEM_TYPE_FOOD:
                            item = new Item();
                            item.itemInfo = DataManager.gI().getFoodInfo(idItem);
                            break;
                        case Item.ITEM_TYPE_TOY:
                            item = new Toy(idItem);
                            break;
                        default:
                            item = new Item();
                            break;
                    }
                    item.m_bItemType = typeItem;
                    item.m_iId = idItem;
                    item.m_iNumItem = msg.reader().readShort();
                    vtItems.addElement(item);
                }
                MainScr.gI().onQuestCompleted(questNumber, bonusMoney1, bonusMoney2, bonusXp, vtItems, level);
                break;
                                
            case Cmd_Message.SET_HANDLER:
            	byte handler = msg.reader().readByte();
            	switch(handler){
            	case MainScr.SERVICEID_GAME_BAU_CUA:
            		RoomListScr.m_strTitleGame = "Bầu cua";
            		RoomListScr.m_iIconGame = 15;
            		GameBauCuaMsgHandler.onHandler();
            		break;
            	case MainScr.SERVICEID_GAME_DOAN_THU:
            		GameQuestPetMsgHandler.onHandler();
            		RoomListScr.m_strTitleGame = "Đoán Thú";
            		RoomListScr.m_iIconGame = 16;
            		break;
            	case MainScr.SERVICEID_GAME_DUA_THU:
            		RoomListScr.m_strTitleGame = "Đua Thú";
            		RoomListScr.m_iIconGame = 12;
            		break;
            	case MainScr.SERVICEID_GAME_HOC_TIENG_ANH:
            		GameStudyEnglishMsgHandler.onHandler();
            		RoomListScr.m_strTitleGame = "Học Tiếng Anh";
            		RoomListScr.m_iIconGame = 13;
            		break;
            	case MainScr.SERVICEID_GAME_QUAY_SO:
            		RoomListScr.m_strTitleGame = "Quay Số";
            		RoomListScr.m_iIconGame = 14;
            		break;
            	case MainScr.SERVICEID_GAME_SONG_DAU:
            		GameFightingMsgHandler.onHandler();
            		RoomListScr.m_strTitleGame = "Song Đấu";
            		RoomListScr.m_iIconGame = 11;
            		break;
            		/*
            	case MainScr.SERVICEID_GAME_TIM_CAP:
            		GameTimCapMsgHandler.onHandler();
            		RoomListScr.m_strTitleGame = "Tìm cặp";
            		RoomListScr.m_iIconGame = 17;
            		break;
            		*/
            	case MainScr.SERVICEID_MAIN:
            		miniGameMessageHandler = null;
            		break;
            	default:
            		break;
            	}
            	
            	if(handler == MainScr.SERVICEID_MAIN){
            		GlobalService.gI().getGardenData(NuoiThuMidlet.m_myPlayerInfo.m_iId);
            	}
            	else  if(handler != MainScr.SERVICEID_GAME_QUAY_SO){
            		GlobalService.gI().requestRoomList();
            	}
            	
            	break;
            
            default:
            	if(miniGameMessageHandler != null)
            		miniGameMessageHandler.onMessage(msg);
            	break;
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void readListDrug(Message msg) throws IOException {
		// TODO Auto-generated method stub
    	short count = msg.reader().readShort();
        Vector v = new Vector();
        for (int i = 0; i < count; i++) {
            DrugInfo t = new DrugInfo();

            t.m_bItemType = Item.ITEM_TYPE_DRUG;
            t.m_iId = msg.reader().readShort();
            t.m_strName = msg.reader().readUTF();
            
            t.m_iMoney[0] = msg.reader().readInt();
            t.m_iMoney[1] = msg.reader().readInt();
            t.m_sLevelRequired = msg.reader().readShort();
            t.m_strDes = msg.reader().readUTF();
            t.m_sExperience = msg.reader().readShort();
            t.importFrameImg(msg.reader().readUTF());
            
            v.addElement(t);
        }
        DataManager.gI().saveListDrugInfo(v);
	}

	private void readListFood(Message msg) throws IOException {
		// TODO Auto-generated method stub
		short count = msg.reader().readShort();
        Vector v = new Vector();
        for (int i = 0; i < count; i++) {
            FoodInfo t = new FoodInfo();

            t.m_bItemType = Item.ITEM_TYPE_FOOD;
            t.m_iId = msg.reader().readShort();
            t.m_strName = msg.reader().readUTF();
            t.m_sType = msg.reader().readShort();
            t.m_iMoney[0] = msg.reader().readInt();
            t.m_iMoney[1] = msg.reader().readInt();
            t.m_sLevelRequired = msg.reader().readShort();
            t.m_sHealth = msg.reader().readShort();
            t.m_sExperience = msg.reader().readShort();
            t.importFrameImg(msg.reader().readUTF());
            
            v.addElement(t);
        }
        DataManager.gI().saveListFoodInfo(v);
	}

	private void readListAvatar(Message msg) throws IOException {
		// TODO Auto-generated method stub
		short count = msg.reader().readShort();
        Vector v = new Vector();

        for (int i = 0; i < count; i++) {
            AvatarInfo t = new AvatarInfo();

            t.m_bItemType = Item.ITEM_TYPE_AVATAR;
            t.m_iId = msg.reader().readShort();
            t.m_strName = msg.reader().readUTF();
            t.m_iMoney[0] = msg.reader().readInt();
            t.m_iMoney[1] = msg.reader().readInt();
            t.m_sTimeExpired = msg.reader().readShort();
            t.m_sLevelRequired = msg.reader().readShort();
            t.m_sExperience = msg.reader().readShort();
            t.importFrameImg(msg.reader().readUTF());
            t.m_bIsSell = msg.reader().readBoolean();
            
            v.addElement(t);
        }
        DataManager.gI().saveListAvatarInfo(v);
	}

	private void readListToy(Message msg) throws IOException {
		// TODO Auto-generated method stub
		short count = msg.reader().readShort();
        Vector v = new Vector();

        for (int i = 0; i < count; i++) {
            ToyInfo t = new ToyInfo();

            t.m_bItemType = Item.ITEM_TYPE_TOY;
            t.m_iId = msg.reader().readByte();
            t.m_strName = msg.reader().readUTF();
            t.m_iMoney[0] = msg.reader().readInt();
            t.m_iMoney[1] = msg.reader().readInt();
            t.m_sTimeExpired = msg.reader().readShort();
            t.m_sLevelRequired = msg.reader().readShort();
            t.m_sExperience = msg.reader().readShort();
            
            t.importFrameImg(msg.reader().readUTF());
            t.m_bIsSell = msg.reader().readBoolean();

            v.addElement(t);
        }
        DataManager.gI().saveListToyInfo(v);
	}

	private void readListAnimal(Message msg) throws IOException {
		// TODO Auto-generated method stub
		short count = msg.reader().readShort();
        Vector v = new Vector();

        for (int i = 0; i < count; i++) {
            AnimalInfo animal = new AnimalInfo();

            animal.m_bItemType = Item.ITEM_TYPE_ANIMAL;
            animal.m_iId = msg.reader().readByte();
            animal.m_strName = msg.reader().readUTF();
            animal.m_iMoney[0] = msg.reader().readInt();
            animal.m_iMoney[1] = msg.reader().readInt();
            animal.m_sFood = msg.reader().readShort();
            animal.m_sHealth = msg.reader().readShort();
            animal.m_sExperience = msg.reader().readShort();
            animal.m_sTimeLiving = msg.reader().readShort();
            animal.m_sTimeCreateMoney = msg.reader().readShort();
            animal.m_sTimeFeed = msg.reader().readShort();
            animal.m_sLevelRequired = msg.reader().readShort();
            animal.m_sTimeTakeABath = msg.reader().readShort();
            animal.m_sMoneyHarvest = msg.reader().readShort();
            animal.m_sMoneyPeriod = msg.reader().readShort();
            animal.m_strHead = msg.reader().readUTF();
            animal.m_strBody = msg.reader().readUTF();

            animal.m_strSmallHead = msg.reader().readUTF();
            animal.m_strSmallBody = msg.reader().readUTF();
            animal.importProperties(msg.reader().readUTF());
            
            
            //read head part and body part
            AnimalInfo.importPart(animal.m_headPart[1], animal.m_strHead);
            AnimalInfo.importPart(animal.m_bodyPart[1], animal.m_strBody);
            //AnimalInfo.importSmallPart(animal.m_smallBodyPart, animal.m_strSmallBody);
            //AnimalInfo.importSmallPart(animal.m_smallHeadPart, animal.m_strSmallHead);
            AnimalInfo.importSmallPart(animal.m_bodyPart[0], animal.m_strSmallBody);
            AnimalInfo.importSmallPart(animal.m_headPart[0], animal.m_strSmallHead);
                        
            v.addElement(animal);
        }
        DataManager.gI().saveListAnimalInfo(v);
	}

	public void onConnectOK() {
        // TODO Auto-generated method stub
    }

    public void onConnectionFail() {
        // TODO Auto-generated method stub
        GameCanvas.startOKDlg(GameResource.checkConnection);
    }

    public void onDisconnected() {
        // TODO Auto-generated method stub
        GameCanvas.menu.m_showMenu = false;
        GameCanvas.menu.m_showMenuGarden = false;
    
    	GameCanvas.startOK(GameResource.disconnect, new IAction() {

            public void perform() {
                // TODO Auto-generated method stub
                GameCanvas.loginScr.switchToMe();
                GameCanvas.endDlg();
            }
        });
    
    }
}
